<?php

namespace app\services;

use EasyWeChat\Factory;
use plugin\admin\app\model\Good;
use plugin\admin\app\model\Option;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

class WechatService
{

    public static function index($request)
    {
        $config = self::getConfig();
        $app = Factory::officialAccount($config);
        $symfony_request = new SymfonyRequest($request->get(), $request->post(), [], $request->cookie(), [], [], $request->rawBody());
        $symfony_request->headers = new HeaderBag($request->header());
        $app->rebind('request', $symfony_request);

        $response = $app->server->serve();
        return $response->getContent();
    }


    public static function getConfig()
    {
         $config = Option::where('name', 'wechat')->value('value');
        $config = json_decode($config, true);
        $app_id = $config['appid'];
        $app_secret = $config['appSecret'];
        $config = [
            'app_id' => $app_id,
            'secret' => $app_secret,
            'token' => 'sensheng',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => 'https://car.ydk1.cn/wechat/oauthCallback',
            ],

            //...
        ];
        return $config;
    }

    // 初始化
    public static function init()
    {
        $config = self::getConfig();
        $app = Factory::officialAccount($config);
        return $app;
    }

    public static function login($url)
    {
        $oauth = self::init()->oauth;
        $redirectUrl = $oauth ->scopes(['snsapi_userinfo'])
            ->redirect($url);
        return urldecode($redirectUrl);
    }

    public static function getCodeUrl(string $url)
    {
        $oauth = self::init()->oauth;
        return $oauth->scopes(['snsapi_userinfo'])
            ->redirect($url);
    }

    public static function userFromCode($code)
    {
        $oauth = self::init()->oauth;
        $oauth = $oauth->scopes(['snsapi_base']);
        $user = $oauth->userFromCode($code);
       return $user;
    }

    public static function createMenu($menu)
    {
        $app = self::init();
        $menu = $app->menu->create($menu);
        return $menu;

    }

    public static function buildConfig($APIs)
    {
        $app = self::init();
//        $app->jssdk->setUrl('');
        return $app->jssdk->buildConfig($APIs,false,false,false);

    }

    public function orderUnify($openid,$goods_id)
    {
        $app = self::init();
        $goods = Good::where('unique_id',$goods_id)->first();
        $sn = date('YmdHis').rand(1000,9999);
        $result = $app->order->unify([
            'body' => $goods->name.'-定金支付',
            'out_trade_no' => '20150806125346',
            'total_fee' => $goods->price*100,
            'notify_url' => 'https://car.ydk1.cn/wechat/notify', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $openid,
        ]);
    }



}